//
//  CJCustomGoodsInfoView.m
//  DatangLife
//
//  Created by 娜 on 2018/5/3.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "CJCustomGoodsInfoView.h"
#import "CJCustomGoodsModel.h"
#import "CJCustomSizeAttributeModel.h"
@interface CJCustomGoodsInfoView ()
@property (nonatomic, weak) UIView *backView;
@property (nonatomic, weak) UIImageView *goodsImageView;

@property (nonatomic, strong) UILabel *goodsTitleLable;
//@property (nonatomic, strong) UILabel *goodsCountLable;
@property (nonatomic, strong) UILabel *goodsPriceLable;


@end


@implementation CJCustomGoodsInfoView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    CGFloat goodsimgH = kSize(95);
    CGFloat margin = kSize(15);
    CGFloat leftImgaeMargin = kSize(10);
    CGFloat heig = kSize(20);
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(margin);
        make.left.right.bottom.equalTo(self);
    }];
    
    [self.goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self).offset(margin);
        make.width.height.equalTo(@(goodsimgH));
    }];
    
    [self.goodsTitleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(kSize(5));
        make.left.equalTo(self.goodsImageView.mas_right).offset(leftImgaeMargin);
        make.right.equalTo(self.backView).offset(-margin);
        make.height.equalTo(@(heig*2.0));
    }];
    
    [self.goodsPriceLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.goodsTitleLable);
        make.top.equalTo(self.goodsTitleLable.mas_bottom);
        make.height.equalTo(@(heig));
    }];
    
//    [self.goodsCountLable mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(self.goodsTitleLable);
//        make.top.equalTo(self.goodsPriceLable.mas_bottom);
//        make.height.equalTo(@(heig));
//    }];
    
//    [self.promatLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(self.goodsTitleLable);
//        make.top.equalTo(self.goodsPriceLable.mas_bottom);
//        make.height.equalTo(@(heig));
//    }];
    
}

- (void)initData:(CJCustomGoodsModel *)model{
    _model = model;
    NSLog(@"%@",model.price.isCustome);
    [self.goodsImageView setImageWithUrlString:[NSString stringWithFormat:@"%@%@",kPublicImageInterfaceAPI,model.imageId] palceHolderImage:kPlaceholderImage];
    self.goodsTitleLable.text = model.title;
//    self.goodsCountLable.text = [NSString stringWithFormat:@"库存：%@",model.totalStock];
    if (model.isTeZhi) {
        self.goodsPriceLable.text = [NSString stringWithFormat:@"%@",model.price.minPrice];
    }else{
        if ([model.price.minOriginalPrice floatValue] == 0) {
            self.goodsPriceLable.text = [NSString stringWithFormat:@"¥%@",model.price.minPrice];
        }else{
            self.goodsPriceLable.text = [NSString stringWithFormat:@"¥%@¥%@",model.price.minPrice,model.price.minOriginalPrice];
            NSMutableAttributedString *attritu = [[NSMutableAttributedString alloc]initWithString:self.goodsPriceLable.text];
            [attritu addAttributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleThick), NSForegroundColorAttributeName: [UIColor lightGrayColor],NSBaselineOffsetAttributeName:@(0),
                                     NSFontAttributeName: [UIFont systemFontOfSize:13]
                                     } range:[self.goodsPriceLable.text rangeOfString:[NSString stringWithFormat:@"¥%@",model.price.minOriginalPrice]]];
            self.goodsPriceLable.attributedText = attritu;
        }
    }
    
    
    
}

- (void)resetData:(CJCustomSizeAttributeModel *)sizeModel{
    
//    [self.goodsImageView setImage:[UIImage imageNamed:sizeModel.imageId]];
    [self.goodsImageView setImageWithUrlString:[NSString stringWithFormat:@"%@%@",kPublicImageInterfaceAPI,sizeModel.imageId] palceHolderImage:kPlaceholderImage];
    
//    self.goodsCountLable.text = [NSString stringWithFormat:@"库存：%@",sizeModel.stock];
    self.goodsPriceLable.text = [NSString stringWithFormat:@"¥%@ ¥%@",sizeModel.price,sizeModel.originalPrice];
    NSMutableAttributedString *attritu = [[NSMutableAttributedString alloc]initWithString:self.goodsPriceLable.text];
    [attritu addAttributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleThick), NSForegroundColorAttributeName: [UIColor lightGrayColor],NSBaselineOffsetAttributeName:@(0),
                             NSFontAttributeName: [UIFont systemFontOfSize:13]
                             } range:[self.goodsPriceLable.text rangeOfString:[NSString stringWithFormat:@"¥%@",sizeModel.originalPrice]]];
    self.goodsPriceLable.attributedText = attritu;
    
}

//- (UILabel *)promatLabel{
//    if (!_promatLabel) {
//        UILabel *promatL = [[UILabel alloc] init];
//        promatL.textAlignment = NSTextAlignmentLeft;
//        promatL.font = kFont(14.f);
//        promatL.textColor = [UIColor colorWithHexString:kDetailTextGrayBlackColor];
//        promatL.numberOfLines = 1;
//        promatL.text = @"";
//        [self addSubview:promatL];
//        _promatLabel = promatL;
//    }
//    return _promatLabel;
//}

- (UILabel *)goodsPriceLable{
    if (!_goodsPriceLable) {
        UILabel *priceL = [[UILabel alloc] init];
        priceL.textAlignment = NSTextAlignmentLeft;
        priceL.font = kFont(14.f);
        priceL.textColor = [UIColor colorWithHexString:kNomalTextBlackColor];
        priceL.numberOfLines = 1;
        priceL.text = @"000";
        [self addSubview:priceL];
        _goodsPriceLable = priceL;
    }
    return _goodsPriceLable;
}


//- (UILabel *)goodsCountLable{
//    if (!_goodsCountLable) {
//        UILabel *countL = [[UILabel alloc] init];
//        countL.textAlignment = NSTextAlignmentLeft;
//        countL.font = kFont(14.f);
//        countL.textColor = [UIColor colorWithHexString:kDetailTextGrayBlackColor];
//        countL.numberOfLines = 1;
//        countL.text = @"库存";
//        [self addSubview:countL];
//        _goodsCountLable = countL;
//    }
//    return _goodsCountLable;
//}


- (UILabel *)goodsTitleLable{
    if (!_goodsTitleLable) {
        UILabel *titL = [[UILabel alloc] init];
        titL.textAlignment = NSTextAlignmentLeft;
        titL.font = kFont(15.f);
        titL.textColor = [UIColor colorWithHexString:kNomalTextBlackColor];
        titL.numberOfLines = 2;
        titL.text = @"标题";
        [self addSubview:titL];
        _goodsTitleLable = titL;
    }
    return _goodsTitleLable;
}

- (UIView *)backView{
    if (!_backView) {
        UIView *backV = [[UIView alloc] init];
        backV.backgroundColor = [UIColor whiteColor];
        [self addSubview:backV];
        _backView = backV;
    }
    return _backView;
}

- (UIImageView *)goodsImageView{
    if (!_goodsImageView) {
        UIImageView *imageV = [[UIImageView alloc] init];
        imageV.contentMode = UIViewContentModeScaleAspectFill;
        imageV.clipsToBounds = YES;
        imageV.layer.borderColor = kCommonRedColor.CGColor;
        imageV.layer.borderWidth = 2;
        [self addSubview:imageV];
        _goodsImageView = imageV;
    }
    return _goodsImageView;
}


@end
